package com.model;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity(name = "Employees")
public class Employee implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int id;

	private String firstname;

	private String lastname;

	private String area;

	private String image = "";

	private String username;

	private String password = "";

	private Group group;

	private long mobileNumber;

	private int internNumber;

	private int monthsOfSeniority;

	private Date startDate;

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	private String email;

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Employee() {
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@ManyToOne(targetEntity = Group.class, fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public long getMobileNumber() {
		return mobileNumber;
	}

	public void setMobileNumber(long mobileNumber) {
		this.mobileNumber = mobileNumber;
	}

	public int getInternNumber() {
		return internNumber;
	}

	public void setInternNumber(int internNumber) {
		this.internNumber = internNumber;
	}

	public int getMonthsOfSeniority() {
		Date today = Calendar.getInstance().getTime();
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setTime(today);
		if (startDate !=null) {
			cal2.setTime(startDate);
			long milis1 = cal1.getTimeInMillis();
			long milis2 = cal2.getTimeInMillis();
			long diff = milis1 - milis2;
			long days = (diff / (24 * 60 * 60 * 1000));
			
			return (int) days/30;
		}
		return 0;
	}

	public void setMonthsOfSeniority(int monthsOfSeniority) {
		this.monthsOfSeniority = monthsOfSeniority;
	}

	@Override
	public boolean equals(Object obj) {

		if ((obj instanceof Integer)) {
			Integer a = (Integer) obj;
			return this.id == a;
		}
		return super.equals(obj);
	}

	@Override
	public int hashCode() {
		int hash = 1;
		return hash * 31 + firstname.hashCode();
	}

}
